﻿using System;
using System.ServiceModel;

using TableTop.DataModel;
using TableTop.DataModel.Shelf;

namespace TableTop.TestConsoleApplication.Network
{

    [ServiceContract(Namespace="http://www.tabletop.net/TestProgram/Server")]
    public interface IProviderServerInterface
    {
        [OperationContract]
        // void GetSerializedSheet(SerializedSheet Sheet);
        void GetSerializedSheet(TransportableSerializer<ICharacterSheetData> Sheet);
    }
        
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class ProviderServer
        : MarshalByRefObject, IProviderServerInterface, IDisposable
    {
        private NetTcpBinding Binding = null;
        public const String str_Address = "net.tcp://localhost:50000";
        private ServiceHost shCurrentHost = null;

        public ProviderServer()
        {
            this.Binding = new NetTcpBinding();
            this.shCurrentHost = new ServiceHost(this);
            this.shCurrentHost.AddServiceEndpoint(typeof(IProviderServerInterface), this.Binding, str_Address);
        }

        public void Listen()
        {
            this.shCurrentHost.Open();
        }

        public void Close()
        {
            this.shCurrentHost.Close();
        }

        // public void GetSerializedSheet(SerializedSheet Sheet)
        public void GetSerializedSheet(TransportableSerializer<ICharacterSheetData> Sheet)
        {
            String strSheetReceived = String.Empty;
            ICharacterSheetData icsSheet = null;

            icsSheet = Sheet.Deserialize();

            strSheetReceived = String.Format("Character sheet received:" +
                                             Environment.NewLine +
                                             "Game Assembly: {0}" +
                                             Environment.NewLine +
                                             "Sheet type name: {1}" +
                                             Environment.NewLine +
                                             "Sheet XML Data: {2}" +
                                             Environment.NewLine +
                                             Environment.NewLine +
                                             "Sheet" +
                                             Environment.NewLine +
                                             "--------------------" +
                                             Environment.NewLine +
                                             "{3}" +
                                             Environment.NewLine,
                                             Sheet.AssemblyName,
                                             Sheet.TypeName,
                                             Sheet.DataXML,
                                             icsSheet.ToString());
            Console.WriteLine(strSheetReceived);
        }

        #region IDisposable Members

        public void Dispose()
        {
            this.Binding = null;
            this.shCurrentHost.Close();
            this.shCurrentHost = null;
        }

        #endregion
    }   
}
